package com.sys.controller;

import com.sys.dto.PageResult;
import com.sys.entity.BizTask;
import com.sys.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/task")
public class TaskController {

    @Autowired
    private TaskService taskService;

    @GetMapping("/list")
    public List<BizTask> getTasks(@RequestParam(required = false) String domain,
            @RequestParam(required = false) Integer difficulty,
            @RequestParam(required = false) Integer age) {
        return taskService.findTasks(domain, difficulty, age);
    }

    /**
     * 分页查询任务列表
     * @param domain 领域
     * @param difficulty 难度
     * @param age 年龄
     * @param pageNum 页码（从1开始）
     * @param pageSize 每页大小
     * @return 分页结果
     */
    @GetMapping("/page")
    public PageResult<BizTask> getTasksPage(
            @RequestParam(required = false) String domain,
            @RequestParam(required = false) Integer difficulty,
            @RequestParam(required = false) Integer age,
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "20") Integer pageSize) {
        return taskService.findTasksPage(domain, difficulty, age, pageNum, pageSize);
    }

    @PostMapping("/addCustom")
    public BizTask addCustomTask(@RequestBody BizTask task) {
        // Here you might want to set the creator ID from the logged-in user
        return taskService.addCustomTask(task);
    }
    @PostMapping("/modifyCustom")
    public BizTask modifyCustomTask(@RequestBody BizTask task) {
        return  taskService.modifyCustomTask(task);
    }

    @PostMapping("/init")
    public String initTasks() {
        taskService.initTasksFromExcel();
        return "Tasks initialized successfully";
    }
}